<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($specialist!=''){
	$querySpecialist = " AND SpecialistId = '".$specialist."' ";
}else{
	$querySpecialist = "";
}

	$sql="SELECT `day`, `score.marketshare`, `fee.information`, `fee.shout`, `fee.transaction`, `fee.profit` FROM x_score WHERE ExperimentId = '".$Experiment."' ".$querySpecialist." ORDER BY day ASC LIMIT ".$start.",".$items.";";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$marketshare = array();
	$efficiency = array();
	$feeInformation = array();
	$feeShout = array();
	$feeTransaction = array();
	$feeProfit = array();
	/*
	if($start==0){
		$marketshare[0] = 0;
		$convergenceCoefficient[0] = 0;
		$feeInformation[0] = 0;
		$feeShout[0] = 0;
		$feeTransaction[0] = 0;
		$feeProfit[0] = 0;
		$i=1;
	}else{
		$i=0;
	}
	*/
	$i=0;
	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$marketshare[$i] = round($row['score.marketshare'], 3);
		$feeInformation[$i] = round($row['fee.information'], 3);
		$feeShout[$i] = round($row['fee.shout'], 3);
		$feeTransaction[$i] = round($row['fee.transaction'], 3);
		$feeProfit[$i] = round($row['fee.profit'], 3);
		$i++;		
	} 

$line_marketshare = new line_hollow();
$line_marketshare->set_width( 1 );
$line_marketshare->set_colour( '#5EA725' );
$line_marketshare->set_dot_size( 3 );
$line_marketshare->set_values( $marketshare );
$line_marketshare->set_tooltip( 'Marketshare: #val#' );
$line_marketshare->set_key( "Marketshare", 10 );

$line_feeInformation = new line_dot();
$line_feeInformation->set_width( 1 );
$line_feeInformation->set_colour( '#AEA725' );
$line_feeInformation->set_dot_size( 3 );
$line_feeInformation->set_values( $feeInformation );
$line_feeInformation->set_tooltip( 'Fee Information: #val#' );
$line_feeInformation->set_key( "Fee Information", 10 );

$line_feeShout = new line_dot();
$line_feeShout->set_width( 1 );
$line_feeShout->set_colour( '#A66765' );
$line_feeShout->set_dot_size( 3 );
$line_feeShout->set_values( $feeShout );
$line_feeShout->set_tooltip( 'Fee Shout: #val#' );
$line_feeShout->set_key( "Fee Shout", 10 );

$line_feeTransaction = new line_dot();
$line_feeTransaction->set_width( 1 );
$line_feeTransaction->set_colour( '#A667A5' );
$line_feeTransaction->set_dot_size( 3 );
$line_feeTransaction->set_values( $feeTransaction );
$line_feeTransaction->set_tooltip( 'Fee Transaction: #val#' );
$line_feeTransaction->set_key( "Fee Transaction", 10 );

$line_feeProfit = new line_dot();
$line_feeProfit->set_width( 1 );
$line_feeProfit->set_colour( '#5E4725' );
$line_feeProfit->set_dot_size( 3 );
$line_feeProfit->set_values( $feeProfit );
$line_feeProfit->set_tooltip( 'Fee Profit: #val#' );
$line_feeProfit->set_key( "Fee Profit", 10 );


$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.' - Charge  -  '.$specialist.' ('.$start.' - '.$final.')' ) );

$x_legend = new x_legend( 'Day' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Percentage' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x_labels = new x_axis_labels();
$x_labels->set_steps( $step );
//$x_labels->set_vertical();
$x_labels->set_colour( '#000000' );
//$x_labels->set_labels( $items );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );

$chart->set_x_axis( $x );

$maxMarketshare = max($marketshare);
$maxFeeInformation = max($feeInformation);
$maxFeeTransaction = max($feeTransaction);
$maxFeeShout = max($feeShout);
$maxFeeProfit = max($feeProfit);
$maxY = max($maxMarketshare, $maxFeeInformation, $maxFeeTransaction, $maxFeeShout, $maxFeeProfit);
$maxY = ceil($maxY * 10)/10;

$divisorY = $maxY * 20 ;
$stepY = $maxY / $divisorY;

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( 0, $maxY, $stepY ); 

//
// here we add our data sets to the chart:
//
$chart->add_element( $line_marketshare );
$chart->add_element( $line_feeInformation );
$chart->add_element( $line_feeTransaction );
$chart->add_element( $line_feeShout );
$chart->add_element( $line_feeProfit );

echo $chart->toPrettyString();

?>
